---
title: 'Section 2: Empirical Support for Key Assumptions'
author: "Milan Svolik"
date: "4/20/2023"
output:
  pdf_document: default
  html_document:
    df_print: paged
---

```{r setup, include=FALSE}
rm(list = ls(all = TRUE))
library(tidyverse)
library(ggplot2)
library(estimatr)
library(ggExtra)

set.seed(5877)
```

# Figure 2.1
* The distribution of the approval difference between President Erdogan (AKP) and the CHP's mayoral candidate Ekrem Imamoglu
```{r}
# Load the 2019 experiment data
rm(list = ls(all = TRUE))
load("data/df_experiments.RData")

df_plot <- df_experiments %>%
  mutate(latent=approval_erdogan-approval_imamoglu)
  
df_plot %>% ggplot(aes(x = latent)) +
      geom_histogram(mapping = aes(y=..count../sum(..count..)), binwidth = 1, colour = "black", fill = "white") +
  theme_minimal() + 
  labs(
       x = "Difference in approval: Erdo\u011fan (AKP) - \u0130mamo\u011flu (CHP) ", y = "Fraction",
       title = ""
    )

ggsave("figures, appendix/approval_histogram.pdf", width = 8, height = 6, units = "in", dpi = 600)
```

# Figure 2.2
* The difference in a respondent's approval of Erdogan and Imamoglu and the vote for the AKP, CHP, or abstention in the 2019 Turkish local election
```{r}
df_plot <- df_experiments %>%
  filter(vote_march2019=="AKP" | vote_march2019=="CHP" |  vote_march2019=="Abstain") %>% 
  mutate(latent=approval_erdogan-approval_imamoglu) %>%
  filter(!is.na(latent)) %>% 
  group_by(latent) %>% 
  summarize(N = n(),
    abstain = sum(vote_march2019=="Abstain", na.rm=TRUE)/N,
    AKP = sum(vote_march2019=="AKP", na.rm=TRUE)/N,
    CHP = sum(vote_march2019=="CHP", na.rm=TRUE)/N) %>% 
  arrange(latent) %>%
  gather(outcome, fraction, abstain:CHP, factor_key=TRUE)

df_plot %>% 
  ggplot(aes(x=as.factor(latent), y=fraction, col=outcome)) +
  coord_cartesian(ylim = c(0, 1)) +
  geom_hline(yintercept=0) +
  geom_hline(yintercept=0.5, col="grey50") +
  geom_hline(yintercept=1) +
  theme_minimal() +
  geom_line(aes(col=outcome, group=outcome, linetype = outcome),  size=1) + 
  geom_point(aes(col=outcome, shape=outcome), size=2) +
  scale_shape_manual(name  ="", values=c(17, 16, 15), labels=c("Vote AKP","Vote CHP", "Abstain"), breaks=c("AKP","CHP", "abstain")) +   scale_linetype_manual(name  ="", values=c("solid", "dashed", "dotted"), labels=c("Vote AKP","Vote CHP", "Abstain"), breaks=c("AKP","CHP", "abstain")) + 
  scale_color_manual(name  ="", values=c("blue", "red","black"),  labels=c("Vote AKP","Vote CHP", "Abstain"), breaks=c("AKP","CHP", "abstain")) + 
  scale_x_discrete( 
    labels=seq(-10,10,1)) + 
    labs(
       x = "Difference in approval: Erdo\u011fan (AKP) - \u0130mamo\u011flu (CHP) ", y = "Fraction",
       title = ""
    )

ggsave("figures, appendix/approval_difference.pdf", width = 8, height = 6, units = "in", dpi = 600)
```

# Figure 2.3
* A respondent's approval of Erdogan, Imamoglu, and the vote for the AKP, CHP, or abstention in the 2019 Turkish local election
```{r}
df_plot <- df_experiments %>%
  select(vote_march2019, approval_erdogan, approval_imamoglu) %>%
  filter(vote_march2019=="AKP" | vote_march2019=="CHP" |  vote_march2019=="Abstain") %>% 
  filter(!is.na(approval_erdogan) | !is.na(approval_imamoglu))


plot_temp <- df_plot %>%
  ggplot(aes(x = approval_erdogan, y=approval_imamoglu, col=vote_march2019, shape=vote_march2019)) +
  coord_fixed(ratio = 1) +
  theme_classic() + 
  theme(legend.position="left") + 
  geom_jitter(width = .5, size=2) +
    scale_color_manual(name  ="", values=c("blue", "red","black"),  labels=c("Vote AKP","Vote CHP", "Abstain"), breaks=c("AKP","CHP", "Abstain")) +
  scale_shape_manual(name  ="", values=c(17, 16, 15), labels=c("Vote AKP","Vote CHP", "Abstain"), breaks=c("AKP","CHP", "Abstain")) +
  scale_x_continuous( 
    labels=seq(0,10,1), breaks=seq(0,10,1)) +
  scale_y_continuous( 
    labels=seq(0,10,1), breaks=seq(0,10,1)) +
  labs(
       x = "Erdo\u011fan (AKP)", y = "\u0130mamo\u011flu (CHP)",
       title = ""
    )

ggMarginal(plot_temp, type = "histogram", fill="transparent", bins = 10)
ggsave("figures, appendix/erdogan_imamoglu.pdf", width = 6, height = 6, units = "in", dpi = 600)
```

